package com.crocodileQ.blog.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.crocodileQ.blog.domain.BlogRole;
import com.crocodileQ.blog.domain.dto.BlogUserDto;
import com.crocodileQ.blog.domain.vo.BlogUserVo;
import com.crocodileQ.common.core.domain.entity.SysRole;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.crocodileQ.common.annotation.Log;
import com.crocodileQ.common.core.controller.BaseController;
import com.crocodileQ.common.core.domain.AjaxResult;
import com.crocodileQ.common.enums.BusinessType;
import com.crocodileQ.blog.domain.BlogUser;
import com.crocodileQ.blog.service.IBlogUserService;
import com.crocodileQ.common.utils.poi.ExcelUtil;
import com.crocodileQ.common.core.page.TableDataInfo;

/**
 * 用户管理Controller
 * 
 * @author crocodilePole
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/blog/blogUser")
public class BlogUserController extends BaseController
{
    @Autowired
    private IBlogUserService blogUserService;

    /**
     * 查询用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:blogUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogUser blogUser)
    {
        startPage();
        List<BlogUserVo> list = blogUserService.selectBlogUserList(blogUser);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:blogUser:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogUser blogUser)
    {
        List<BlogUserVo> list = blogUserService.selectBlogUserList(blogUser);
        ExcelUtil<BlogUserVo> util = new ExcelUtil<>(BlogUserVo.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:blogUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AjaxResult ajax = AjaxResult.success();
        BlogUserVo blogUserVo = blogUserService.selectBlogUserById(id);
        ajax.put("roleIds", blogUserVo.getBlogRole().stream().map(BlogRole::getId).collect(Collectors.toList()));
        ajax.put(AjaxResult.DATA_TAG, blogUserVo);
        return ajax;
    }

    /**
     * 新增用户管理
     */
    @PreAuthorize("@ss.hasPermi('blog:blogUser:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogUser blogUser)
    {
        return toAjax(blogUserService.insertBlogUser(blogUser));
    }

    /**
     * 修改用户管理
     */
    @PreAuthorize("@ss.hasPermi('blog:blogUser:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogUserDto blogUserDto)
    {


        return toAjax(blogUserService.updateBlogUser(blogUserDto));
    }

    /**
     * 删除用户管理
     */
    @PreAuthorize("@ss.hasPermi('blog:blogUser:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(blogUserService.deleteBlogUserByIds(ids));
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('blog:blogUser:remove')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{ids}")
    public  AjaxResult resetPwd(@PathVariable Long[] ids)
    {
        return toAjax(blogUserService.resetPwdByIds(ids));
    }
}
